#import "UITextView+Category.h"
#import <objc/runtime.h>

@implementation UITextView (Category)

+ (void)load
{
    method_exchangeImplementations(class_getInstanceMethod(NSClassFromString(@"UITextView"), NSSelectorFromString(@"setText:")), class_getInstanceMethod(NSClassFromString(@"UITextView"), NSSelectorFromString(@"ex_setText:")));
    method_exchangeImplementations(class_getInstanceMethod(NSClassFromString(@"UITextView"), NSSelectorFromString(@"layoutSubviews")), class_getInstanceMethod(NSClassFromString(@"UITextView"), NSSelectorFromString(@"ex_layoutSubviews")));
    
}

#pragma mark - ************* ex ******************

- (void)ex_setText:(NSString *)text
{
    [self ex_setText:text];
    if (self.placeholder)
    {
        [self updatePlaceHolder];
    }
}

- (void)ex_layoutSubviews
{
    [self ex_layoutSubviews];
    if (self.placeholder)
    {
        UIEdgeInsets textContainerInset = self.textContainerInset;
        CGFloat lineFragmentPadding = self.textContainer.lineFragmentPadding;
        CGFloat x = lineFragmentPadding + textContainerInset.left + self.layer.borderWidth;
        CGFloat y = textContainerInset.top + self.layer.borderWidth;
        CGFloat width = CGRectGetWidth(self.bounds) - x - textContainerInset.right - 2*self.layer.borderWidth;
        CGFloat height = [self.placeHolderLabel  sizeThatFits:CGSizeMake(width, 0)].height;
        self.placeHolderLabel.frame = CGRectMake(x, y, width, height);
    }
}

#pragma mark - ************* Associated ******************
- (void)setPlaceholder:(NSString *)placeholder
{
    objc_setAssociatedObject(self, @selector(placeholder), placeholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self updatePlaceHolder];
}

- (NSString *)placeholder
{
    return objc_getAssociatedObject(self, @selector(placeholder));
}

- (UILabel *)placeHolderLabel
{
    UILabel *placeHolderLab = objc_getAssociatedObject(self, @selector(placeHolderLabel));
    if(!placeHolderLab)
    {
        placeHolderLab = [[UILabel alloc] init];
        placeHolderLab.text = self.placeholder;
        placeHolderLab.numberOfLines = 0;
        placeHolderLab.textAlignment = self.textAlignment?:NSTextAlignmentLeft;
        placeHolderLab.font = self.font?:[UIFont systemFontOfSize:14];
        placeHolderLab.textColor = [UIColor grayColor];
        objc_setAssociatedObject(self, @selector(placeHolderLabel), placeHolderLab, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
       
        __weak typeof(self) weakSelf = self;
        [[NSNotificationCenter defaultCenter] addObserver:weakSelf selector:@selector(updatePlaceHolder) name:UITextViewTextDidChangeNotification object:weakSelf];
    }
    
    return placeHolderLab;
}

- (void)setPlaceHolderLabel:(UILabel *)placeHolderLabel
{
    
}


- (void)updatePlaceHolder
{
    if (self.text.length)
    {
        if (self.placeHolderLabel.superview)
        {
            [self.placeHolderLabel removeFromSuperview];
        }
    }
    else
    {
        if (!self.placeHolderLabel.superview)
        {
            [self insertSubview:self.placeHolderLabel atIndex:0];
        }
    }
}

@end
